package news.controller;

import news.service.AddTopicServiceImpl;
import news.service.IAddTopicService;
import news.service.IQueryUserService;
import news.service.QueryUserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;


/**
 * @author Administrator
 * 添加主题
 */
@WebServlet("/checkUsername")
public class CheckUsernameController extends HttpServlet {
    private IQueryUserService iQueryUserService = new QueryUserServiceImpl();
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置响应内容类型
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("utf-8");
        String username = request.getParameter("username");
        PrintWriter out = response.getWriter();
        String res = "";

        int count = iQueryUserService.countByName(username);
        if (count > 0) {
            //如果用户存在
            res = "{\n" +
                    "            \"status\" : 200 ,\n" +
                    "            \"msg\" : \"success\"\n" +
                    "        }";

        } else {
            res = "{\n" +
                    "            \"status\" : 200 ,\n" +
                    "            \"msg\" : \"fail\"\n" +
                    "        }";
        }
        out.write(res);
        out.flush();
        out.close();

    }

}